/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.tags;

import ca.trader.comment.Comment;
import ca.trader.logger.TraderLogger;
import ca.trader.sale.Sale;
import ca.trader.utils.HttpConsts;
import ca.trader.utils.TraderStringUtils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.ocpsoft.prettytime.PrettyTime;

/**
 *
 * @author Ahsan
 */
public class PrintSale extends SimpleTagSupport
{

	 private static final String _className = PrintSale.class.getName();
	 private Sale sale;
	 private static final int COL_1_MAX_WIDTH = 10;
	 private static final int COL_1_MIN_WIDTH = 5;
	 private static final int COL_2_MAX_WIDTH = 70;
	 private static final int COL_2_MIN_WIDTH = 30;
	 private boolean favouritedByAuthenticatedUser;

	 private static SimpleDateFormat COMMENT_DATE_FORMAT = new SimpleDateFormat("MMM dd, yyyy hh:mm a");

	 public boolean isFavouritedByAuthenticatedUser()
	 {
		  return favouritedByAuthenticatedUser;
	 }

	 public void setFavouritedByAuthenticatedUser(boolean favouritedByAuthenticatedUser)
	 {
		  TraderLogger.logInfo(_className, "setFavouritedByAuthenticatedUser", "Sale favourited: " + favouritedByAuthenticatedUser);
		  this.favouritedByAuthenticatedUser = favouritedByAuthenticatedUser;
	 }

	 private static final SimpleDateFormat saleDateFormat = new SimpleDateFormat("MMMMM d, yyyy h:mm a");

	 private JspWriter writer;

	 public Sale getSale()
	 {
		  return sale;
	 }

	 public void setSale(Sale sale)
	 {
		  this.sale = sale;
	 }

	 /**
	  * Called by the container to invoke this tag. The implementation of this
	  * method is provided by the tag library developer, and handles all tag
	  * processing, body iteration, etc.
	  */
	 @Override
	 public void doTag() throws JspException
	 {
		  final String _methodName = "doTag";
		  writer = getJspContext().getOut();

		  try
		  {
			   writer.println("<h3>" + sale.getTitle() + "</h3><a id=\"aFavouriteSale\" href=\"FavouriteSaleForAuthenticatedUser?saleID=" + sale.getId() + " \">");

			   if (favouritedByAuthenticatedUser)
			   {
				    writer.println("Unfavourite this sale");
			   }
			   else
			   {
				    writer.println("Favourite this sale");
			   }

			   writer.println("</a><br/><br/><table border=\"1\">");

			   String rowTemplate = getTableRowTemplate();

			   printRow(rowTemplate, "Created", getCreationDateString());
			   printRow(rowTemplate, "Modified", getModifiedDateString());
			   printRow(rowTemplate, "Price", sale.getPrice().toPlainString());
			   printRow(rowTemplate, "Location", sale.getLocation().toString());
			   printRow(rowTemplate, "Category", sale.getCategory().getName());
			   printRow(rowTemplate, "Seller", getSellerLink());

			   writer.println("</table>");

			   writer.println("<p>" + TraderStringUtils.newlineToBr(sale.getDescription()) + "</p>");

			   if (sale.getCommentList().size() > 0)
			   {
				    String commentTemplate = getCommentTemplate();
				    writer.println("<h3>Comments:</h3> <table border=\"1\">");

				    for (Comment comment : sale.getCommentList())
				    {
						writer.println (String.format (commentTemplate, comment.getCommentor(), getCommentDateString(comment.getCommentDate()), TraderStringUtils.newlineToBr(comment.getCommentContent())));
				    }

				    writer.println("</table>");
			   }
		  }
		  catch (java.io.IOException ex)
		  {
			   TraderLogger.logSevere(_className, _methodName, "Unknown exception", ex);
		  }
	 }

	 private String getCommentTemplate()
	 {
		  return String.format("<tr><td>By <b>%%s</b> on <i>%%s</i>:<br/>%%s</td></tr>");
	 }

	 private String getTableRowTemplate()
	 {
		  return String.format("<tr><th style=\"width:%d%%%%;max-width:%d%%%%;\">%%s</th><td style=\"width:%d%%%%;max-width:%d%%%%;\">%%s</td></tr>",
				COL_1_MAX_WIDTH, COL_1_MIN_WIDTH, COL_2_MAX_WIDTH, COL_2_MIN_WIDTH);
	 }

	 private void printRow(String rowTemplate, String arg1, String arg2) throws IOException
	 {
		  writer.println(String.format(rowTemplate, arg1, arg2));
	 }

	 private String getSellerLink()
	 {
		  return String.format("<a href=\"%s?id=%d\">%s</a>", HttpConsts.VIEW_USER_PROFILE_JSP_PAGE, sale.getSellerID(), sale.getSeller());
	 }

	 private String getCreationDateString()
	 {
		  return getDateString(sale.getCreationDate());
	 }

	 private String getModifiedDateString()
	 {
		  return getDateString(sale.getModifiedDate());
	 }

	 private String getDateString(Date then)
	 {
		  PrettyTime currentTime = new PrettyTime(new Date());
		  return saleDateFormat.format(then) + " (" + currentTime.format(then) + ")";
	 }

	 private String getCommentDateString(Date commentTime)
	 {
		  return COMMENT_DATE_FORMAT.format(commentTime);
	 }

}
